package order;

import controller.SessionAttribute;
import entity.OrdersLocal;
import entity.OrdersLocalHome;
import java.io.IOException;
import javax.ejb.FinderException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import transfer.User;

/**
 *
 * @author Oleh
 */
public class ViewOrder extends HttpServlet {

    public static final String emailParameter = "email";
    public static final String subscribedParameter = "subscribed";
    public static final String orderParameter = "order";

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute(SessionAttribute.USER);

            String email = request.getParameter(emailParameter);
            Boolean subscribed = Boolean.valueOf(request.getParameter(subscribedParameter));
            Integer orderId = Integer.valueOf(request.getParameter(orderParameter));

            System.out.println("email = " + email);
            System.out.println("subscribed = " + subscribed);
            System.out.println("orderId = " + orderId);

            InitialContext context = new InitialContext();
            OrdersLocalHome homeOrder = (OrdersLocalHome) context.lookup("java:comp/env/Orders");
            OrdersLocal localOrder = (OrdersLocal) homeOrder.findByPrimaryKey(orderId);
            transfer.Order order = localOrder.getOrder();

            if (user != null) {
                order.setIsSubscribed(subscribed);
            } else {
                if (email != null && email != "")
                    order.setEmail(email);
                order.setIsSubscribed(subscribed);
            }
            
            localOrder.setOrder(order);

        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        } 
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
